/*
    Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "Game.h"

#include "./scene/LevelInfo.h"
#include "./scene/GameLevelScene.h"
#include "Monster.h"
#include "MainPlayer.h"
#include "./scene/StartScene.h"
#include "./scene/LogoScene.h"

Game::Game() : m_currentGameLevel(0), m_currentLevelInfo(nullptr)
{
}

void Game::statrGame()
{
    GameWorld::getInstance()->replaceScene(LogoScene::create()->convertSceneSharedPtr());
    GameWorld::getInstance()->getScheduler()->addSchedulerTaskDelay([this]()
                                                                    { this->resetGame(); },
                                                                    "SADASS", 4 * 1000);
}

void Game::nextGameLevelScene()
{
    if (m_currentGameLevel >= MAX_LEAVEL)
    {
        this->resetGame();
    }
    else
    {
        m_currentGameLevel++;
        m_currentLevelInfo = nullptr;
        GameWorld::getInstance()->replaceScene(GameLevelScene::create()->convertSceneSharedPtr());
    }
}

void Game::resetGame()
{
    m_currentGameLevel = 0;
    m_currentLevelInfo = nullptr;

    GameWorld::getInstance()->replaceScene(StartScene::create()->convertSceneSharedPtr());
}

IGameLevelInfo *Game::getCurrentLevelInfo()
{
    if (m_currentLevelInfo)
    {
        return m_currentLevelInfo;
    }

    if (m_currentGameLevel == 1)
    {
        return m_currentLevelInfo = m_levelInfo[1].get();
    }
    else if (m_currentGameLevel == 2)
    {
        return m_currentLevelInfo = m_levelInfo[2].get();
    }
    else if (m_currentGameLevel == 3)
    {
        return m_currentLevelInfo = m_levelInfo[3].get();
    }

    return m_currentLevelInfo = nullptr;
}

int Game::getCurrentGameLevel() const
{
    return m_currentGameLevel;
}

bool Game::loadResource()
{
    //加载游戏资源
    auto cache = GameWorld::getInstance()->getDefultSpriteFrameCache();
    cache->addSpriteFramesWithImagePack(_R("ui.xml"), _R("ui.png"));
    cache->addSpriteFrame(SpriteFrame::createFromPicture(R(L"start-bg.jpg")), L"start_bg");
    cache->addSpriteFrame(SpriteFrame::createFromPicture(R(L"background.png")), L"background");
    cache->addSpriteFramesWithImagePack(_R("relo.xml"), _R("role.png"));
    cache->addSpriteFramesWithImagePack(_R("checkBox.xml"), _R("checkBox.png"));
    cache->addSpriteFrame(SpriteFrame::createFromPicture(R(L"shpe.png")), L"shpe");
    cache->addSpriteFrame(SpriteFrame::createFromPicture(R(L"text.png")), L"text");
    cache->addSpriteFrame(SpriteFrame::createFromPicture(R(L"logo_bg.png")), L"logo_bg");

    //加载声音
    Monster::initAudio();
    MainPlayer::initAudio();

    m_buttonAudio = AudioManager::getInstance()->loadAudio(R(L"start_but.mp3"));
    if (!AudioManager::getInstance()->isValidId(m_buttonAudio))
    {
        // return false;
    }

    m_bgAudio = AudioManager::getInstance()->loadAudio(R(L"bg.mp3"));
    if (!AudioManager::getInstance()->isValidId(m_bgAudio))
    {
        // return false;
    }

    loadLevel(_R("level_conf.xml"));

    return true;
}

void Game::playAudio(AudioType type)
{
    if (type == AudioType::BG_AUDIO)
    {
        GameWorld::getInstance()->getAudioManager()->playAudio(m_bgAudio, true);
    }
    else if (type == AudioType::BUTTON_AUDIO)
    {
        GameWorld::getInstance()->getAudioManager()->playAudio(m_buttonAudio);
    }
}

void Game::stopAudio(AudioType type)
{
    if (type == AudioType::BG_AUDIO)
    {
        GameWorld::getInstance()->getAudioManager()->stopAudio(m_bgAudio);
    }
    else if (type == AudioType::BUTTON_AUDIO)
    {
        GameWorld::getInstance()->getAudioManager()->stopAudio(m_buttonAudio);
    }
}

void Game::loadLevel(const std::wstring &file)
{
    tinyxml2::XMLDocument doc;
    auto err = doc.LoadFile(StringUtility::wcharToChar(file.c_str()).c_str());
    if (err != tinyxml2::XML_SUCCESS)
    {
        assert(false);
        return;
    }

    tinyxml2::XMLElement *root = doc.RootElement();

    auto level_info = root->FirstChildElement("level_info");
    while (level_info)
    {
        int level = level_info->FirstChildElement("level")->IntText();
        int a_count_monster = level_info->FirstChildElement("moster_count_a")->IntText();
        int b_count_monster = level_info->FirstChildElement("moster_count_b")->IntText();
        int boos_count_monster = level_info->FirstChildElement("moster_count_boss")->IntText();

        auto p = std::make_shared<LevelInfo>();
        p->level = level;
        p->monsterACount = a_count_monster;
        p->monsterBCount = b_count_monster;
        p->boosCount = boos_count_monster;

        m_levelInfo.emplace(std::make_pair(level, std::move(p)));

        level_info = level_info->NextSiblingElement();
    }
}
